package com.qingzhu.controller.orderserver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingzhu.pojo.CustomPage;
import com.qingzhu.pojovo.OrderVo;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.service.OrderService;
import com.qingzhu.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/order/select")
public class OrderSelectController extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    OrderService orderService = new OrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("订单查询服务..");

        String server = req.getParameter("server");
        if (server.equals("find")){
            findOrderAll(req,resp);
        }else if (server.equals("search")) {
            searckOrder(req,resp);
        }else {
            resp.getWriter().write(objectMapper.writeValueAsString(new ResultVo(1001,"未接收到正确的服务器标记")));
        }
    }


    // 订单查询
    public void findOrderAll(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String activeName = req.getParameter("activeName");
        String userId = req.getParameter("userId");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        CustomPage<OrderVo> customPage = orderService.findOrderAll(activeName,Integer.parseInt(userId),
                Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        ResultVo resultVo = customPage != null ? new ResultVo(200,customPage) :
                new ResultVo(1000,"没有历史记录");
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
    // 订单搜索
    public void searckOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String selectType = req.getParameter("selectType");
        String searchInput = req.getParameter("searchInput");
        String userId = req.getParameter("userId");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        System.out.println("订单搜索类型："+selectType+" 关键字："+searchInput+" 用户id："+userId);
        CustomPage<OrderVo> customPage = orderService.searckOrder(selectType, searchInput,Integer.parseInt(userId),
                Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        ResultVo resultVo = customPage != null ? new ResultVo(200,customPage) :
                new ResultVo(1000,"没有历史记录");
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
}
